import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.regex.Pattern;

public class UserInterface extends JFrame {

	private static final int HEIGHT = 700;
	private static final int WIDTH = 600;

	private static JTextField sourceFileNameField;
	private JButton runProgramButton;
	private JTextArea outputArea;
	private JPanel interfacePanel;
	private String outputAreaText;
	private String coreInstruction;
	private String dataInstruction;
	private static Main main;
	private UserInterfaceContent uiContent;

	// Button handler
	private RunButtonHandler runHandler;

	public UserInterface() {

		sourceFileNameField = new JTextField(10);
		runProgramButton = new JButton("Run");
		outputArea = new JTextArea(30, 30);
		runHandler = new RunButtonHandler();
		runProgramButton.addActionListener(runHandler);

		interfacePanel = new JPanel();
		interfacePanel.setLayout(new FlowLayout());
		interfacePanel.add(sourceFileNameField);
		interfacePanel.add(runProgramButton);
		interfacePanel.add(outputArea);

		getContentPane().add(interfacePanel);
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		UserInterface uInterface = new UserInterface();
	}

	private class RunButtonHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				Pattern p = Pattern.compile(" ");
				String[] files = p.split(sourceFileNameField.getText());
				main = new Main();
				main.go(files);
				uiContent = new UserInterfaceContent(files);
				outputArea.setText(uiContent.createOutput());
			} catch (Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		}
	}

	public String getCoreInstruction() {
		return coreInstruction;
	}

	public String getDataInstruction() {
		return dataInstruction;
	}
}
